<template>
	<view class="content">
		<view class="" style="padding: 20upx 40upx;">
			<u-search searchIconSize="0" placeholder="请输入活动名称" label="可修改:" v-model="title" maxlength="18" :showAction="false"
				shape="square" bgColor="#ffffff">
			</u-search>
		</view>

		<view class="troopNum_list">
			<view :class="troopIndex==item.team?'select_ troopNum_list_item':'troopNum_list_item'"
				v-for="(item ,index) in troopNumList" @click="troopIndex=item.team">
				{{item.title}}
			</view>
		</view>
		<view class="" style="font-size: 24upx;color: #999999;text-align: right;margin: 10upx 40upx;">
			（每队需4-10人）
		</view>
		<view class="cooperationType" v-if="troopIndex!=1">
			<view :class="cooperationIndex==index?'select cooperationType_item':'cooperationType_item'"
				v-for="(item ,index) in cooperationType" @click="cooperationIndex=index">

				<text>{{item.title}}</text>
				<u-icon name="question-circle-fill" @click="setCooperationTypeShow(index)"></u-icon>
			</view>
		</view>
		<view class="goods_title">
			<text>已购挑战</text>
			<text @click="reset">重置</text >
		</view>
		<view class="goods_list">
			<view class="goods_item" v-for="(item ,index) in myGoodsList" @click="selectGoode(item)">
				
				<view class="wrap" v-if="item.id===4"><span class="silk-ribbon">试玩</span></view>
				
				<view class="image">
					<image :src="item.pic">
						<view class="" v-if="item.rankingNum">
							<text>{{item.rankingNum}}</text>
						</view>

				</view>
				<view class="">
					{{item.title}}
				</view>
			</view>


		</view>
		
		
		<u-popup :show="sexShow" mode="center" round="12rpx" closeable @close="sexShow=false">
			<view class="sex">
				<view class="text">
					请选择您的性别
				</view>
				<view class="sex_list">
					<view :class="sex==1?'active':''" style="margin-right: 64upx;" @click="sex=1">
						<u-icon name="man" color="#91BCE6" size="120rpx" :customStyle="{fontWeight:700}"></u-icon>
					</view>
					<view :class="sex==2?'active':''" style="margin-left: 64upx;" @click="sex=2">
						<u-icon name="woman" color="#FFA5D9" size="120rpx" :customStyle="{fontWeight:700}">></u-icon>
					</view>
				</view>
				<view class="join_game" @tap="$u.throttle(establish, 500)">
					创建活动
				</view>
			</view>
		</u-popup>
		

		<view class="bottom">
			<view class="room" @click="myRoom">
				我的活动
			</view>
			<view class="btn" @click="establishOf">
				创建活动
			</view>
		</view>



		<u-modal :show="cooperationTypeShow" :title="explain.title" :content='explain.content' confirmText="我知道了"
			confirmColor="#FED194" @confirm="cooperationTypeShow=false"></u-modal>
	</view>



</template>

<script>
	export default {
		data() {
			return {
				title: '',
				troopNumList: [{
					title: '两队',
					team: 2
				}, {
					title: '单队',
					team: 1
				}, {
					title: '三队',
					team: 3
				}, {
					title: '四队',
					team: 4
				}],
				troopIndex: 2,
				sexShow:false,
				sex:1,
				cooperationType: [{
					title: '自由分组'
				}, {
					title: '随机分组'
				}],
				cooperationIndex: 1,
				myGoodsList: [],
				selectGoodeList: [],
				cooperationTypeShow: false,
				libertyTeam: {},
				restrainTeam: {},
				explain: {}
			}
		},
		watch: {
			troopIndex() {
				this.upData()
			}
		},
		created() {
			this.title=`团建活动${this.$u.timeFormat(Date.parse(new Date()), 'yyyymmddhhMMss')}`
			this.$myRuquest('home/index/account_det', {
				show_zt: 2
			}).then(res => {
				this.libertyTeam = res.data
			})
			this.$myRuquest('home/index/account_det', {
				show_zt: 3
			}).then(res => {
				this.restrainTeam = res.data
			})
			this.upData()
		},
		methods: {
			upData() {
				this.$myRuquest('home/game/game', {
					team: this.troopIndex == 1 ? 1 : 2
				}).then(res => {
					this.selectGoodeList=[]
					this.myGoodsList = res.data
				})
			},
			myRoom(){
				uni.navigateTo({
					url:'/pages/me/activityList/activityList'
				})
			},
			setCooperationTypeShow(index) {
				this.explain = index ? this.restrainTeam : this.libertyTeam
				this.cooperationTypeShow = true
			},
			selectGoode(item) {
				let selectGoodeList = this.selectGoodeList
				let Xindex = -1
				let msg=''
				if ((item.type == 11||item.type == 12) && selectGoodeList.length)  msg='bingo 不可与其他游戏混合！'
				
				selectGoodeList.forEach(el=>{
					if(el.type==11||el.type==12) msg='bingo 不可与其他游戏混合！'
				})
				
				
				selectGoodeList.forEach((el, index) => {
					if (el.id == item.id) Xindex = index
				})
				if (Xindex == -1) {	
					if(msg)return uni.showToast({
						title: 'bingo 不可与其他游戏混合！',
						icon:'none'
					});
					selectGoodeList.push(item)
				} else {
					selectGoodeList.splice(Xindex, 1)
					//取消元素后 整体重排
					this.myGoodsList.forEach(el => {
						el.rankingNum = 0
					})
				}
				this.selectGoodeList = selectGoodeList
				//排序
				let rankingNum = 1
				selectGoodeList.forEach(els => {
					let code = rankingNum
					this.myGoodsList.forEach(el => {
						if (el.id == els.id) {
							el.rankingNum = rankingNum
							rankingNum++
						}
					})
				})

			},
			reset() {
				this.selectGoodeList = []
				this.myGoodsList.forEach(el => {
					el.rankingNum = 0
				})
			},
			establishOf(){

				if(!this.selectGoodeList.length) return uni.showToast({
					title: '请选择挑战项目',
					icon:'none'
				})
				let code=false
				for (let i = 0; i < this.title.length; i++) {
					let item=this.title[i]
					if(!(uni.$u.test.chinese(item)||uni.$u.test.enOrNum(item))) {
						code=true
						i=this.title.length
					} 
				}

				if(code) return uni.showToast({
					title: '活动名称禁止特殊字符！',
					icon:'none'
				})
				
				if (this.title.length > 18) return uni.showToast({
					title: '活动名称最多18个字',
					icon:'none'
				})
				this.sexShow=true
			},
			establish() {
	
				
				
				let data = {
					title: this.title,
					team: this.troopIndex,
					team_type: this.cooperationIndex + 1,
					activ_id: [],
					sex:this.sex
				}
				this.selectGoodeList.forEach(item => {
					data.activ_id.push(item.id)
				}),
				data.activ_id = data.activ_id.join(',')

				this.$myRuquest('home/game/game_add', data).then(res => {
					this.$myRuquest('home/index/config').then(ress => {
						uni.setStorageSync('video_s', ress.data.video_s);
						uni.setStorageSync('video', ress.data.video);
						uni.setStorageSync('gameDataVideoId', '');
						uni.redirectTo({
							url: '/pages/createActivity/formTeam?gameId=' + res.data.game_id + '&team_type=' + data
								.team_type
						})
					})

				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #FAFAFA;
		padding-bottom: 160upx;
		// height: calc(100vh - 180rpx);
	}

	.troopNum_list {
		display: flex;
		justify-content: space-between;
		padding: 0upx 40upx;

		.troopNum_list_item {
			padding: 10upx 40upx;
			background-color: #fff;
			border-radius: 12upx;
			font-size: 28upx;
			transition: all .5s;
		}
	}

	.cooperationType {
		display: flex;
		justify-content: space-around;
		padding: 0upx 40upx;

		.cooperationType_item {
			display: flex;
		}

		.cooperationType_item>text {
			padding: 10upx 40upx;
			background-color: #fff;
			border-radius: 30upx;
			font-size: 28upx;
			transition: all .5s;
			margin-right: 10upx;
		}
	}

	.select_,
	.select>text {
		background-color: #FED194 !important;
		color: #AC7D3D;
	}

	.goods_title {
		display: flex;
		justify-content: space-between;
		font-size: 28upx;
		color: #333333;
		font-weight: 700;
		padding: 30upx 20upx;
		padding-bottom: 0;
	}

	.goods_list {
		display: flex;
		flex-wrap: wrap;

		.goods_item {
			width: 30%;
			margin: 20upx 1.6%;
			text-align: center;
			position: relative;
			
			
			.wrap {
				width: 100%;
				height: 188rpx;
				position: absolute;
				top: -8rpx;
				left: 8rpx;
				overflow: hidden;
			}
			
			.wrap:before {
				content: "";
				display: block;
				border-radius: 8rpx 8rpx 0rpx 0rpx;
				width: 40rpx;
				height: 8rpx;
				position: absolute;
				right: 85rpx;
				background: #AC7D3D;
			}
			
			.wrap:after {
				content: "";
				display: block;
				border-radius: 0rpx 8rpx 8rpx 0rpx;
				width: 8rpx;
				height: 40rpx;
				position: absolute;
				right:1rpx;
				top: 82rpx;
				background: #AC7D3D;
			}
			
			.silk-ribbon {
				display: inline-block;
				text-align: center;
				width: 200rpx;
				height: 40rpx;
				line-height: 40rpx;
				position: absolute;
				top: 12rpx;
				right: -53rpx;
				z-index: 2;
				overflow: hidden;
				transform: rotate(45deg);
				border: 1rpx dashed;
				box-shadow: 0 0 0 3rpx $theme-color, 0rpx 21rpx 5rpx -18rpx rgba(0, 0, 0, 0.6);
				background: $theme-color;
				color:#ffffff
			}
			

			.image {
				width: 100%;
				height: 200upx;
				border-radius: 8upx;
				overflow: hidden;
				position: relative;

				image {
					width: 100%;
					height: 100%;
				}

				>view {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					background-color: rgba(0, 0, 0, .59);
					align-items: center;
					line-height: 100%;
					z-index: 1;
					font-size: 48upx;
					color: #FED194;
					font-weight: 700;

					text {
						position: absolute;
						top: 50%;
						left: 50%;
						transform: translate(-50%, -50%);
					}
				}
			}

			view {
				font-size: 28upx;
				color: #333333;
			}
		}
	}

	.bottom {
		padding: 24upx 20upx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 9999;
		width: calc(100vw - 40upx);
		display: flex;
		align-items: center;
		.room{
			width: 200upx;
			margin-right: 20upx;
			text-align: center;
			background: #dcdcdc;
			color: #999;
			height: 80upx;
			line-height: 80upx;
			border-radius: 8rpx;
		}

		.btn {
			flex: 1;
			height: 80upx;
			margin: 0 auto;
			background-color: #FED194;
			color: #AC7D3D;
			// border-radius: 50upx;
			font-size: 36upx;
			line-height: 80upx;
			text-align: center;
			border-radius: 8rpx;
		}
	}
	
	
	
	.sex {
		padding: 0 132upx;
	
		.text {
			font-size: 36upx;
			color: #333333;
			font-weight: 700;
			text-align: center;
			padding: 40upx 0;
		}
	
		.sex_list {
			display: flex;
			justify-content: space-between;
	
			view {
				width: 140upx;
				height: 140upx;
				background-color: #E8E8E8;
				border-radius: 8upx;
			}
	
			.active {
				border: 2upx solid #91BCE6;
			}
		}
	
		.join_game {
			width: 234upx;
			height: 64upx;
			line-height: 64upx;
			text-align: center;
			border-radius: 64upx;
			margin: 0 auto;
			margin-top: 56upx;
			margin-bottom: 20upx;
			font-size: 28upx;
			color: #AC7D3D;
			background-color: #FED194;
		}
	}
</style>
